kmpkg_check_linkage(ONLY_STATIC_LIBRARY)

kmpkg_from_gitee(
    OUT_SOURCE_PATH SOURCE_PATH
    REPO kumo-hub/protobuf
    REF "v${VERSION}"
    SHA512 a1ccdad27771b3ed721ceb607c570b690f08bbef032548e449c357d4da2d1ee86e22e3fe4a4f9b20e55b6253e85e46225a70c08e0404c8b06386f1f7a34c3fb7
    HEAD_REF main
    PATCHES
        fix-cmake.patch
        fix-NAN-on-Win11.patch
)

kmpkg_check_features(
    OUT_FEATURE_OPTIONS FEATURE_OPTIONS
    FEATURES
        codegen KMPKG_UPB_BUILD_CODEGEN
)

if(NOT KMPKG_UPB_BUILD_CODEGEN)
    kmpkg_add_to_path(PREPEND "${CURRENT_HOST_INSTALLED_DIR}/tools/protobuf" "${CURRENT_HOST_INSTALLED_DIR}/tools/upb")
endif()

kmpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}/upb/cmake"
    OPTIONS ${FEATURE_OPTIONS}
)

kmpkg_cmake_install(ADD_BIN_TO_PATH)
kmpkg_cmake_config_fixup()

if (KMPKG_UPB_BUILD_CODEGEN)
    kmpkg_copy_tools(
        AUTO_CLEAN
        TOOL_NAMES
            protoc-gen-upbdefs
            protoc-gen-upb
            protoc-gen-upb_minitable
    )
else()
    configure_file("${CMAKE_CURRENT_LIST_DIR}/upb-config-kmpkg-tools.cmake" "${CURRENT_PACKAGES_DIR}/share/upb/upb-config-kmpkg-tools.cmake" @ONLY)
endif()

file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share" "${CURRENT_PACKAGES_DIR}/debug/include")

kmpkg_copy_pdbs()

kmpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")
